//
//  CuoTiShiTiXiangQingVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/6/20.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "CuoTiShiTiXiangQingVC.h"

@interface CuoTiShiTiXiangQingVC ()
{
    NSDictionary *mainStudentNumDic;
}
@end

@implementation CuoTiShiTiXiangQingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    _studentsArray = [NSMutableArray new];
    self.view.userInteractionEnabled = YES;
    self.view.backgroundColor = CONTENTBACKCOLOR;
    [self.view addSubview:self.backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@24);
        make.width.equalTo(@50);
        make.height.equalTo(@20);
    }];
    [self.view addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.height.equalTo(@15);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.backButton.mas_centerY);
    }];
    
    [self.view addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@64);
        make.bottom.equalTo(@0).offset(-20);
    }];
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@" 返回" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColorChange colorwithHexString:@"#20B097"] forState:UIControlStateNormal];
        [sender setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        sender.titleLabel.font = [UIFont systemFontOfSize:12];
        [sender addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = sender;
    }
    return _backButton;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(618,34.5,63,15);
        label.numberOfLines = 0;
        label.textAlignment = 1;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"试题详情" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        label.attributedText = string;
        _titleLabel = label;
    }
    return _titleLabel;
}
- (void)backClick:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(APP_WIDTH, 0, MAINW, self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0) style:UITableViewStyleGrouped];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[RSDCell class] forCellReuseIdentifier:@"RSDCell"];
        [tableView registerClass:[RSDCNextCell class] forCellReuseIdentifier:@"RSDCNextCell"];
        [tableView registerClass:[CuoTiStudentCell class] forCellReuseIdentifier:@"CuoTiStudentCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 3) {
        int num = _mainModel.dataStudentsArray.count%5;
        if (num>0) {
            return _mainModel.dataStudentsArray.count/5+1;
        }else{
            return _mainModel.dataStudentsArray.count/5;
        }
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_mainModel == nil) {
        return 0;
    }
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        UILabel *label = [UILabel new];
        label.attributedText = [self attributedStringWithHTMLString:[NSString stringWithFormat:@"%@/n/n",_mainModel.analysisContent]];
        CGFloat height = [label.attributedText boundingRectWithSize:CGSizeMake(self.tableList.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        return height+50;
    }else if (indexPath.section == 1){
        UILabel *label = [UILabel new];
        label.attributedText = [self attributedStringWithHTMLString:[NSString stringWithFormat:@"%@",_mainModel.rightAnswerContent]];
        CGFloat height = [label.attributedText boundingRectWithSize:CGSizeMake(self.tableList.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        return height+50;
    }else if (indexPath.section == 3){
//        float h = _mainModel.dataStudentsArray.count/5;
//        NSLog(@"个数 %f",h);
//        if (_mainModel.dataStudentsArray.count%5>=1) {
//            return (h+1)*50;
//        }
        return 50;
    }else{
        //0
        UILabel *label = [UILabel new];
        label.attributedText = [self attributedStringWithHTMLString:[NSString stringWithFormat:@"%@/n/n",_mainModel.questionContent]];
        CGFloat height = [label.attributedText boundingRectWithSize:CGSizeMake(self.tableList.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        return height+50;
    }
    return 150;
}
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        RSDCNextCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"RSDCNextCell" forIndexPath:indexPath];
        if (!cell2) {
            cell2 = [[RSDCNextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RSDCNextCell"];
        }
        [cell2.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.analysisContent] baseURL:nil];
        cell2.shiTiLabel.text = [NSString stringWithFormat:@"试题:%@",_questionType];
        cell2.duiLabel.text = [NSString stringWithFormat:@"做对:%@",_mainModel.rightNum];
        cell2.cuoLabel.text = [NSString stringWithFormat:@"做错:%@",_mainModel.wrongNum];
        cell2.zhengQueLabel.text = [NSString stringWithFormat:@"正确率:%@%%",_mainModel.rightRate];
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell2;
    }else if(indexPath.section == 3){
        //学生
        CuoTiStudentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CuoTiStudentCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CuoTiStudentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CuoTiStudentCell"];
        }
       
        cell.dataArray = [mainStudentNumDic objectForKey:[NSString stringWithFormat:@"%d",(int)indexPath.row]];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell layoutView];
        return cell;
    }else{
        RSDCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"RSDCell" forIndexPath:indexPath];
        if (!cell1) {
            cell1 = [[RSDCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RSDCell"];
        }
        if (indexPath.section == 0) {
            [cell1.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.questionContent] baseURL:nil];
        }else if (indexPath.section == 1){
            [cell1.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.rightAnswerContent] baseURL:nil];
        }else if (indexPath.section == 2){
            
        }
        cell1.backgroundColor = [UIColor whiteColor];
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell1;
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    mainView.backgroundColor = CONTENTBACKCOLOR;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    label.backgroundColor = CONTENTBACKCOLOR;
    label.textColor = TITLECOLOR;
    label.font = [UIFont systemFontOfSize:14];
    label.text = @" 变通";
    if (section == 0) {
        label.text = @" 题目";
    }else if (section == 1){
        label.text = @" 答案";
    }else if (section == 2){
        label.text = @" 解析";
    }else{
        label.text = @" 错题学生";
    }
    label.textAlignment = 0;
    [mainView addSubview:label];
    return mainView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
- (NSMutableDictionary *)chuLiShuZu:(NSMutableArray *)array{
    NSMutableDictionary *dic = [NSMutableDictionary new];
    int fiveCount = (int)array.count/5;
    int yuShu = array.count%5;
    for (int j =0;j<fiveCount;j++) {
        NSMutableArray *resultArray = [NSMutableArray new];
        for (int i = 0; i<5; i++) {
            [resultArray addObject:array[j*5+i]];
        }
        [dic setValue:resultArray forKey:[NSString stringWithFormat:@"%d",j]];
    }
    NSMutableArray *resultArray = [NSMutableArray new];
    if (yuShu>0) {
        for (int i = 0; i<yuShu; i++) {
            [resultArray addObject:array[fiveCount*5+i]];
        }
        [dic setValue:resultArray forKey:[NSString stringWithFormat:@"%d",fiveCount]];
    }
//    NSLog(@"得到的数组 %@",dic);
    return dic;
}
- (void)reloadView{
    mainStudentNumDic = [NSDictionary new];
     mainStudentNumDic = [self chuLiShuZu:_mainModel.dataStudentsArray];
    [self.tableList reloadData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
